package com.example.androidtest1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.androidtest1.adapter.MyAdapter;
import com.example.androidtest1.bean.User;
import com.example.androidtest1.dialog.MyDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
//这个程序还有一个可以继续书写的，就是可以把我们的多项选择先隐藏掉，通过长按而弹出
public class ListViewActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener{
    private ListView lv1=null;
    private MyAdapter adapter =null;
    private TextView tv1 =null;
    private Button btn1,btn2 =null;
    private List<User> lists =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        lv1=findViewById(R.id.lv1);
        tv1 =findViewById(R.id.add);
        btn1 =findViewById(R.id.delete);
        btn2 =findViewById(R.id.alladd);
        tv1.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        lists=new ArrayList<User>();
        lists.add(new User("张三"));
        lists.add(new User("李四"));
        lists.add(new User("小王"));
        adapter =new MyAdapter(this);
        adapter.setData(lists);
        lv1.setAdapter(adapter);
        lv1.setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (view.getTag() instanceof MyAdapter.ViewHolder){
            MyAdapter.ViewHolder holder =(MyAdapter.ViewHolder) view.getTag();
            holder.checkBox.toggle();
//            holder.textView.setBackgroundResource(R.color.purple_500);
        }

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add:
                //通过添加，提供dialog，让用户自己输入名字，并且把名字传入User对象
                MyDialog dialog =new MyDialog(ListViewActivity.this);
                dialog.show();
                StringBuffer result = new StringBuffer("");
                dialog.getWindow().findViewById(R.id.dialog_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        result.append(dialog.getResult());
                        Log.i("hello",result.toString());
                        adapter.addData(new User(result.toString()));
                        adapter.notifyDataSetChanged();
                    }
                });
                break;
            case R.id.delete:
                //这个地方的列表

                Map<Integer,Boolean> ischeck_delete =adapter.getMap();
                int count =adapter.getCount();
                for (int i=0;i<count;i++){
                    int position =i-(count-adapter.getCount());
                    if (ischeck_delete.get(i)!=null &&ischeck_delete.get(i)){
                        ischeck_delete.remove(i);
                        adapter.removeData(position);
                    }
                }
                btn2.setText("全选");
                adapter.notifyDataSetChanged();
                break;
            case R.id.alladd:
                Map<Integer,Boolean> ischeck =adapter.getMap();
                if (btn2.getText().toString().equals("全选")){
                    adapter.initCheck(true);
                    adapter.notifyDataSetChanged();
                    btn2.setText("全不选");
                }else if (btn2.getText().toString().equals("全不选")){
                    adapter.initCheck(false);
                    adapter.notifyDataSetChanged();
                    btn2.setText("全选");
                }
                break;
            default:
                break;
        }

    }

}